package com.street.controller.admin;

import com.street.constant.JwtClaimsConstant;
import com.street.dto.EmpLoginDTO;
import com.street.entity.Employee;
import com.street.entity.Shop;
import com.street.properties.JwtProperties;
import com.street.result.Result;
import com.street.service.EmployeeService;
import com.street.utils.JwtUtil;
import com.street.vo.EmpLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result login(@RequestBody EmpLoginDTO empLoginDTO){
        Employee employee = employeeService.login(empLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmpLoginVO employeeLoginVO = EmpLoginVO.builder()
                .id(employee.getId())
                .username(employee.getUsername())
                .name(employee.getName())
                .shopId(employee.getShopId())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    @GetMapping("/getShop")
    public Result<Shop> selectShopByUsername(String username){
        Shop shop= employeeService.selectShopByUsername(username);
        return Result.success(shop);
    }


}
